﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.ObjectModel;
using BrainTechLLC.ImageOperators;
using BrainTechLLC.WPF;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using BrainTechLLC;
using CPPClassLibrary;
using DirectoryViewModels;
using ImageAppDataModels;
using ImageAppViewModels;
using System.IO;
using System.Collections.Generic;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;
using System.Drawing;
using System.Windows.Media;
using System.Reflection;
using BinaryNorthwest;
using BrainTechLLC.FilterExtensions;
using System.Windows.Controls;
using System.ServiceModel;
using System.Threading;
using System.ServiceModel.Description;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window, IMainWindow, IDataProvider, IFollowInitializationSteps
    {
        public List<string> Status = new List<string>();
        public static Window1 MainWindow;
        public GroupCollectionViewModel AllGroups;

        public MenuItem StatusMenuItem;
        protected object _lock = new object();
        public DirectoriesViewWindow directoriesView;
        public event EventHandler ImageDeleted;
        public bool MakeFolderExplorerVisible;

        public SpecialFolderHandler SpecialFolderHandler;
        public FilterCollectionHandler FilterHandler;

        public SettingsWatcher SettingsWatcher;
        public DataLoader Loader;
        public DirectoriesToExamineViewModel CurrentDirectoriesToExamine;
        public DirectoriesToExamineCollectionViewModel DirectoriesCollectionsToExamine;

        public ThreadSafeList<FileImageViewModel> _originalFileImageViewModels;
        public FilterCollectionCollectionView CollectionOfFilterCollections;

        public Window1()
        {
            System.Windows.Forms.Application.EnableVisualStyles();
            InitializeComponent();
            OnConstructor();
            StatusMenuItem = statusMenuItem;
        }

        void Window1_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        protected void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            OnLoadAll();
            OnLoadComplete();
            OnApplySettings();
            OnUISetup();

            if (MakeFolderExplorerVisible)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    SettingsWatcher.SettingsViewModelDynamic.FolderExplorerVisible = true;
                    ShowOrHideFolderExplorer(true);
                });
            }
        }

        private void SetupMainGridRowHeight()
        {
            if (Settings.Values.MainGridRow1Height == 0D)
            {
                filterGrid.Height = 300D;
            }
            else
            {
                filterGrid.Height = Settings.Values.MainGridRow1Height;
            }
        }

        private void SetupDirectoriesViewLocation()
        {
            if (directoriesView != null && directoriesView.IsVisible && Settings.Values.DirectoriesViewLocation != null)
            {
                if (Settings.Values.DirectoryViewWindowState == System.Windows.WindowState.Maximized)
                {
                    directoriesView.WindowState = System.Windows.WindowState.Maximized;
                }
                else if (Settings.Values.DirectoryViewWindowState != System.Windows.WindowState.Minimized)
                {
                    if (Settings.Values.DirectoriesViewLocation.Width == 0)
                    {
                        directoriesView.WindowState = System.Windows.WindowState.Maximized;
                    }
                    else
                    {
                        directoriesView.Left = Settings.Values.DirectoriesViewLocation.Left;
                        directoriesView.Top = Settings.Values.DirectoriesViewLocation.Top;
                        directoriesView.Width = Settings.Values.DirectoriesViewLocation.Width;
                        directoriesView.Height = Settings.Values.DirectoriesViewLocation.Height;
                    }
                }
            }
        }

        private void ShowOrHideCommandBar(bool show)
        {
            topCommandBar.Visibility = show ? Visibility.Visible : Visibility.Collapsed;
        }

        private void ShowOrShowFilters(bool show)
        {
            if (show)
            {
                filterGrid.Visibility = Visibility.Visible;
                filterGrid.Height = Settings.Values.MainGridRow1Height;
            }
            else
            {
                filterGrid.Visibility = Visibility.Collapsed;
                mainGrid.RowDefinitions[1].Height = new GridLength(1.0D, GridUnitType.Auto);
            }
        }

        private void ShowOrHideDirectoryOrganizer(bool show)
        {
            if (show)
            {
                rightContainer.ColumnDefinitions[1].MinWidth = 20D;
                directoryCollections.Visibility = Visibility.Visible;
                splitterBetweenGroupsAndDirectories.Visibility = Visibility.Visible;
                directoriesViewControlContainer.Visibility = System.Windows.Visibility.Visible;
                directoriesViewControlContainer.Width = Settings.Values.FolderListWidth;
                Dispatcher.BeginInvoke(() => directoriesViewControlContainer.Width = double.NaN);
            }
            else
            {
                rightContainer.ColumnDefinitions[1].MinWidth = 0D;
                directoryCollections.Visibility = Visibility.Collapsed;
                splitterBetweenGroupsAndDirectories.Visibility = Visibility.Collapsed;
                directoriesViewControlContainer.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (!Settings.Values.FolderListVisible && !Settings.Values.GroupOrganizerVisible)
            {
                rightContainer.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                rightContainer.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void ShowOrHideGroupOrganizer(bool show)
        {
            if (show)
            {
                rightContainer.ColumnDefinitions[0].MinWidth = 20D;
                groupsViewControl.Visibility = Visibility.Visible;
                //splitterBetweenGroupsAndDirectories.Visibility = Visibility.Visible;
                groupsViewControlContainer.Visibility = System.Windows.Visibility.Visible;
                groupsViewControlContainer.Width = Settings.Values.GroupOrganizerWidth;
                Dispatcher.BeginInvoke(() => groupsViewControlContainer.Width = double.NaN);
            }
            else
            {
                rightContainer.ColumnDefinitions[0].MinWidth = 0D;
                groupsViewControl.Visibility = Visibility.Collapsed;
                //splitterBetweenGroupsAndDirectories.Visibility = Visibility.Collapsed;
                groupsViewControlContainer.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (!Settings.Values.FolderListVisible && !Settings.Values.GroupOrganizerVisible)
            {
                rightContainer.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                rightContainer.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void ShowOrHideFolderExplorer(bool show)
        {
            if (show)
            {
                _FolderHandler.Handler.ClosedEvent -= FolderExplorer_Closed;
                _FolderHandler.Handler.ClosedEvent += new EventHandler(FolderExplorer_Closed);
                Commands.ViewFolderExplorerCommand.Execute(_FolderHandler.Handler, this);
            }
            else if (_FolderHandler.IsLoaded)
            {
                _FolderHandler.Handler.ClosedEvent -= FolderExplorer_Closed;
                _FolderHandler.Handler.Close();
            }
        }

        void FolderExplorer_Closed(object sender, EventArgs e)
        {
            SettingsWatcher.SettingsViewModelDynamic.FolderExplorerVisible = false;
        }

        private void ShowOrHideFullViewer(bool show)
        {
            if (show)
            {
                if (_FileImageCollectionViewModels != null && FileImageCollectionViewModels.Count > 0 && FileImageCollectionViewModels[0].Count > 0)
                {
                    _FullViewerHandler.Handler.OpenWindow(FileImageCollectionViewModels[0][0]);
                }
            }
            else if (_ShowMultipleHandler.IsLoaded)
            {
                _ShowMultipleHandler.Handler.Close();
            }
        }

        private void ShowOrHideMultipleHandler(bool show)
        {
            if (show)
            {
                _ShowMultipleHandler.Handler.Closed -= FilterExplorer_Closed;
                _ShowMultipleHandler.Handler.Closed += new EventHandler(FilterExplorer_Closed);
                Commands.ShowMultipleImagesCommand.Execute(_ShowMultipleHandler.Handler, this);
            }
            else if (_ShowMultipleHandler.IsLoaded)
            {
                _ShowMultipleHandler.Handler.Closed -= FilterExplorer_Closed;
                _ShowMultipleHandler.Handler.Close();
            }
        }

        void FilterExplorer_Closed(object sender, EventArgs e)
        {
            SettingsWatcher.SettingsViewModelDynamic.FilterExplorerVisible = false;
        }

        public void SetCurrentFilterCollection(FilterCollectionViewModel collection)
        {
            if (filterView.filterView.FilterCollectionViewModel != FilterHandler._currentFilterCollection)
            {
                filterView.filterView.FilterCollectionViewModel = FilterHandler._currentFilterCollection;
            }

            FileImageCollectionViewModels.ForEach(o => o.Filters = FilterHandler._currentFilterCollection.Collection);
            FilterHandler.OnFiltersGenerationsChanges(false, false);

            if (collection != null && !string.IsNullOrEmpty(collection.CollectionName))
            {
                Settings.Values.CurrentFilterCollectionName = collection.CollectionName;
            }
        }

        private void SetInitialDataContexts()
        {
            CollectionOfFilterCollections = filterView.collectionCollectionView;
            for (int n = 0; n < CollectionViews.Count; n++)
            {
                var collectionView = CollectionViews[n];
                collectionView.FileImageCollectionViewModel = FileImageCollectionViewModels[n];
            }
            CollectionOfFilterCollections.FilterCollectionCollectionViewModel = FilterHandler._filterCollections;
            topCommandBar.SettingsViewModel = SettingsWatcher.SettingsViewModel;
            mainMenu.DataContext = SettingsWatcher.SettingsViewModel;
            groupsViewControl.GroupCollectionViewModel = AllGroups;
            AllGroups.CurrentlySelectedGroupsInListboxChanged += new EventHandler<ObjectEventArgs<GroupCollectionViewModel>>(AllGroups_CurrentlySelectedGroupsInListboxChanged);
        }

        void AllGroups_CurrentlySelectedGroupsInListboxChanged(object sender, ObjectEventArgs<GroupCollectionViewModel> e)
        {
            if (_FileImageCollectionViewModels != null)
            {
                var vms = FileImageCollectionViewModels[0];
                vms.ForEach(o => o.DisplayingGroups = string.Empty);

                for (int n = 0; n < e.obj.CurrentlySelectedGroupsInListbox.Count; n++)
                {
                    var group = e.obj.CurrentlySelectedGroupsInListbox[n];
                    for (int i = 0; i < group.FileImages.Count; i++)
                    {
                        var fileImage = group.FileImages[i];
                        FileImageViewModel vm = vms.QuickLookupByFileName[fileImage.FileName];
                        if (vm != null)
                        {
                            vm.DisplayingGroups = vm.DisplayingGroups + "," + group.GroupName;
                        }
                    }
                }
            }
        }

        private void LoadPathsToExamine()
        {
            var dirs = DirectoriesToExamineCollectionViewModel.Load(FilePaths._pathToExaminePath, App.DataProvider);

            if (dirs.Count == 0 || dirs[0].Count == 0)
            {
                var pathsToExamine = new string[] { Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) };
                MakeFolderExplorerVisible = true;
                DirectoriesToExamine dir = new DirectoriesToExamine(pathsToExamine) { DataProvider = App.DataProvider };
                dir.Name = "Default Folders";
                dirs.Add(dir);
            }

            DirectoriesCollectionsToExamine = dirs;
            CurrentDirectoriesToExamine = dirs[0];
            if (CurrentDirectoriesToExamine.Name == null)
            {
                CurrentDirectoriesToExamine.Name = "Default Folders";
            }
            CurrentDirectoriesToExamine.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_dirsViewModel_CollectionChanged);
            CurrentDirectoriesToExamine.PropertyChanged += new PropertyChangedEventHandler(CurrentDirectoriesToExamine_PropertyChanged);
            directoryCollections.DirectoriesToExamineCollectionViewModel = DirectoriesCollectionsToExamine;
            DirectoriesCollectionsToExamine.CurrentlySelectedGroupsInListboxChanged += new EventHandler<ObjectEventArgs<DirectoriesToExamineCollectionViewModel>>(DirectoriesCollectionsToExamine_CurrentlySelectedGroupsInListboxChanged);
            //string[] pathsToExamine = ImageAppDataModels.DirectoriesToExamine.Load(FilePaths._pathToExaminePath, App.DataProvider);

            //if (pathsToExamine == null)
            //{
            //    pathsToExamine = new string[] { Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) };
            //    MakeFolderExplorerVisible = true;
            //}

            //CurrentDirectoriesToExamine dirs = new DirectoriesToExamine(pathsToExamine);
            //DirectoriesToExamine = new DirectoriesToExamineViewModel(dirs);
            //CurrentDirectoriesToExamine.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_dirsViewModel_CollectionChanged);
        }

        void CurrentDirectoriesToExamine_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "StartingSelection")
            {
                BeginAddingFolders();
            }
            else if (e.PropertyName == "EndingSelection")
            {
                FinishedAddingFolders();
            }

        }

        void DirectoriesCollectionsToExamine_CurrentlySelectedGroupsInListboxChanged(object sender, ObjectEventArgs<DirectoriesToExamineCollectionViewModel> e)
        {
            var firstSelected = e.obj.FirstOrDefault(o => o.IsSelected);
            if (firstSelected != null)
            {
                CurrentDirectoriesToExamine.CollectionChanged -= _dirsViewModel_CollectionChanged;
                CurrentDirectoriesToExamine.PropertyChanged -= CurrentDirectoriesToExamine_PropertyChanged;
                CurrentDirectoriesToExamine = firstSelected;
                CurrentDirectoriesToExamine.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_dirsViewModel_CollectionChanged);
                CurrentDirectoriesToExamine.PropertyChanged += new PropertyChangedEventHandler(CurrentDirectoriesToExamine_PropertyChanged);
                Window1._doNotStartFileImageCollectionDirectoryReader = false;
                CollectionViews = null;

                _AllFileImageCollectionViewModels.ForEach(o => o._fileImageCollection.ClearAllItems());
                ClearFileImageCollections();
                _FileImageCollectionViewModels = null;

                for (int n = 0; n < Window1.MainWindow.CollectionViews.Count; n++)
                {
                    var collectionView = Window1.MainWindow.CollectionViews[n];
                    var fileImages = Window1.MainWindow.FileImageCollectionViewModels[n];

                    collectionView.FileImageCollectionViewModel = fileImages;
                }

                if (directoriesView != null && directoriesView.directoriesView != null)
                {
                    directoriesView.directoriesView.DirectoriesToExamineViewModel = CurrentDirectoriesToExamine;
                }
                if (_FolderHandler.IsLoaded)
                {
                    _FolderHandler.Handler.Close();
                    _FolderHandler.Handler.OpenWindow(this);
                    _FolderHandler.Handler.DirectoriesToExamineViewModel = CurrentDirectoriesToExamine;
                }
            }
        }

        public void SaveDirectoriesViewSizeAndLocation()
        {
            if (directoriesView != null && Settings.Values.DirectoriesVisible && directoriesView.ActualWidth > 0D && directoriesView.ActualHeight > 0D)
            {
                if (directoriesView.WindowState == System.Windows.WindowState.Normal)
                {
                    Settings.Values.DirectoryViewWindowState = System.Windows.WindowState.Normal;
                    Settings.Values.DirectoriesViewLocation = new Rectangle((int)directoriesView.Left, (int)directoriesView.Top, (int)directoriesView.ActualWidth, (int)directoriesView.ActualHeight);
                }
                else if (directoriesView.WindowState == System.Windows.WindowState.Maximized)
                {
                    Settings.Values.DirectoryViewWindowState = System.Windows.WindowState.Maximized;
                }
            }
        }

        public void SaveAllGroups()
        {
            Loader.SaveGroups(AllGroups);
            Loader.SaveFileImageGroups(AllGroups.AllFileImageGroups);
        }

        private void HandleCleanupAndSave()
        {
            SaveAllGroups();
            SettingsWatcher.SettingsViewModel.ReadSettings(this);

            if (_AllFileImageCollectionViewModels != null)
            {
                string key = "*";
                Settings.Values.OrderedFileNames.RemoveAll(key, s => s.Key == key);
                _AllFileImageCollectionViewModels.ForEach(o => SettingsWatcher.SettingsViewModel.Item.SaveOrdering(o, key, fi => fi.FileName, SettingsWatcher.SettingsViewModel.Item.OrderedFileNames));
            }
            DirectoriesCollectionsToExamine.Save(FilePaths._pathToExaminePath);

            SettingsWatcher.SaveData();

            if (FilterHandler != null)
            {
                FilterHandler.SaveData();
            }
        }

        public ThumbnailView FindThumbView(FileImageViewModel vm)
        {
            if (vm == null)
            {
                return null;
            }
            ThumbnailView found = null;
            for (int n = 0; n < CollectionViews.Count; n++)
            {
                var collectionView = CollectionViews[n];
                found = collectionView.FindThumbView(vm.FileImage);
                if (found != null)
                {
                    break;
                }
            }
            return found;
        }

        public void MarkThumbnailFileNameLocationChanged()
        {
            CollectionViews.ForEach(o => o.MarkThumbnailFileNameLocationChanged());
        }

        public void ShowOrHideDirectories(bool show)
        {
            if (show)
            {
                if (directoriesView == null)
                {
                    lock (_lock)
                    {
                        if (directoriesView == null)
                        {
                            directoriesView = new DirectoriesViewWindow();
                            directoriesView.RequestClose += new EventHandler(directoriesView_RequestClose);
                            directoriesView.Show();
                            SetupDirectoriesViewLocation();
                            directoriesView.directoriesView.DirectoriesToExamineViewModel = CurrentDirectoriesToExamine;
                            if (directoriesView.Width < 200D)
                            {
                                directoriesView.Width = 600D;
                            }
                            if (directoriesView.Height < 100D)
                            {
                                directoriesView.Height = 440D;
                            }
                            directoriesView.WireUpSizeChanged();
                            return;
                        }
                    }

                    directoriesView.Show();
                }
                else
                {
                    directoriesView.Show();
                }
            }
            else
            {
                if (directoriesView != null)
                {
                    directoriesView.Hide();
                }
            }
        }

        void directoriesView_RequestClose(object sender, EventArgs e)
        {
            SettingsWatcher.SettingsViewModelDynamic.DirectoriesVisible = false;
        }

        public void ShowOrHideThumbnails(bool show)
        {
            if (show)
            {
                if (_CollectionViews != null)
                {
                    _CollectionViews[0].Visibility = System.Windows.Visibility.Visible;
                }
            }
            else
            {
                if (_CollectionViews != null)
                {
                    _CollectionViews[0].Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

        public static List<FileImageViewModel> SortBySettingValues(List<FileImageViewModel> list)
        {
            List<SortPropOrFieldAndDirection> sortCriteria = new List<SortPropOrFieldAndDirection>();

            // Set up the sort criteria
            if (!string.IsNullOrEmpty(Settings.Values.SortBy))
            {
                sortCriteria.Add(new SortPropOrFieldAndDirection(Settings.Values.SortBy, Settings.Values.SortDescending));
            }
            if (!string.IsNullOrEmpty(Settings.Values.SortBy2))
            {
                sortCriteria.Add(new SortPropOrFieldAndDirection(Settings.Values.SortBy2, Settings.Values.SortDescending2));
            }

            // Perform the multi-property sort 
            List<FileImageViewModel> sorted = BinaryNorthwest.Sorting.MultiSort(list, sortCriteria);

            return sorted;
        }

        #region IDataProvider Members

        public FileImageCollectionViewModelCollection GetCurrentFileImageCollectionViewModels()
        {
            return FileImageCollectionViewModels;
        }

        #endregion

        private void mainGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Console.WriteLine(e.GetPosition(mainGrid).ToString());
        }

        #region IFollowInitializationSteps Members

        public void OnConstructor()
        {
            InitializePluginDefinitions();

            Extensions.SetOnAllTypes("DefaultDataProvider", App.DataProvider, typeof(FileImage), typeof(DirectoriesToExamine), typeof(AllMetaData), typeof(ThumbnailProvider));
            Extensions.InvokeOnAllTypes("CheckInit", typeof(ImageFiltering));

            MainWindow = this;
        }

        public void OnLoadAll()
        {
            Dispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(Dispatcher_UnhandledException);

            SettingsWatcher = new WpfImageDuplicates.SettingsWatcher(this);
            Loader = new DataLoader(this);
            SettingsWatcher.LoadSettings();
            Loader.LoadMetaData();

            FilterHandler = new FilterCollectionHandler(this);
            SpecialFolderHandler = new SpecialFolderHandler();
            ThumbnailView t = new ThumbnailView();

            LoadPathsToExamine();
            FilterHandler.LoadFilters();
            FilterHandler.VerifyOneFilterCollectionExists();
        }

        public void OnLoadComplete()
        {
            AllGroups = new GroupCollectionViewModel();

            for (int n = 0; n < Loader._groups.Count; n++)
            {
                var group = Loader._groups[n];
                AllGroups.AddOrFindGroup(group.GroupName);
            }

            for (int n = 0; n < Loader._fileImageGroups.Count; n++)
            {
                var fiGroup = Loader._fileImageGroups[n];
                AllGroups.AddFileImageToGroup(fiGroup.FileName, fiGroup.GroupName);
            }

            SetInitialDataContexts();
            SettingsWatcher.SettingsViewModel.ApplySettings(this);

            // directoriesView.Show();

            ShowOrHideDirectories(FileImageCollectionViewModels[0].DirectoriesVisible);
            ShowOrHideThumbnails(FileImageCollectionViewModels[0].ThumbnailsVisible);

            // SetupDirectoriesViewLocation();            
            settingsMenuItem.DataContext = SettingsWatcher.SettingsViewModel;
            ShowOrHideCommandBar(SettingsWatcher.SettingsViewModelDynamic.ShowCommandBar);
            ShowOrShowFilters(SettingsWatcher.SettingsViewModelDynamic.ShowFilters);
            ShowOrHideFullViewer(FileImageCollectionViewModels[0].FullViewerVisible);
            ShowOrHideDirectoryOrganizer(SettingsWatcher.SettingsViewModelDynamic.FolderListVisible);
            ShowOrHideGroupOrganizer(SettingsWatcher.SettingsViewModelDynamic.GroupOrganizerVisible);
            SetupMainGridRowHeight();

            FilterHandler.LoadFilterForSimilarity();

            Settings.Values.PropertyChanged += new PropertyChangedEventHandler(Values_PropertyChanged);

            string[] types = Properties.Settings.Default.UsePlugins.Split(',');

            if (types.Length > 0)
            {
                ScanPlugins(types.ToList(), includeAll: false);
            }

            FileImageCollectionViewModels.ForEach(o => ShowOrHideMultipleHandler(o.FilterExplorerVisible));

            if (_FullViewerHandler.Handlers.Count == 0)
            {
                throw new Exception("No Full Viewer Window Handler found!");
            }

            if (_FolderHandler.Handlers.Count == 0)
            {
                throw new Exception("No Folder Handler found!");
            }

            FilterHandler.SetFilterCollection();
            ShowOrHideFolderExplorer(FileImageCollectionViewModels[0].FolderExplorerVisible);
        }

        public void OnApplySettings()
        {

        }

        public void OnUISetup()
        {
            this.Focus();
        }

        #endregion

        private void splitter1_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
        {
            CollectionViews[0].Visibility = System.Windows.Visibility.Collapsed;
        }

        private void mnuDeleteSettings_Click(object sender, RoutedEventArgs e)
        {
            var result = MessageBox.Show("Are you sure you want to delete all settings-related files & filters?", "Are you sure?", MessageBoxButton.YesNo, MessageBoxImage.Warning);

            if (result == MessageBoxResult.Yes)
            {
                FilePaths.DeleteAll();
                MessageBox.Show("Deleted");
            }
        }

        private void mnuSaveGroups_Click(object sender, RoutedEventArgs e)
        {
            SaveAllGroups();
        }

        private void groupsViewControlContainer_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            System.Windows.Size newSize = e.NewSize;
            if (newSize != null && newSize.Width >= 20 && Settings.Values != null)
            {
                Settings.Values.GroupOrganizerWidth = (int)newSize.Width;
            }
        }

        private void directoriesViewControlContainer_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            System.Windows.Size newSize = e.NewSize;
            if (newSize != null && newSize.Width >= 20 && Settings.Values != null)
            {
                Settings.Values.FolderListWidth = (int)newSize.Width;
            }
        }
    }
}
